/**
 * 系统管理  角色管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="formInline.name" placeholder="输入角色名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="success" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
        <el-button size="small" type="danger" @click="batchDelete()">批量删除</el-button>
        <el-button size="small"  @click="resetPage()">展示所有角色</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table ref="multipleTable" :data="listData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange" size="small" highlight-current-row v-loading="loading">
      <el-table-column align="center" type="selection" width="60"></el-table-column>
      <el-table-column sortable prop="id" label="序号" width="150"></el-table-column>
      <el-table-column sortable prop="name" label="角色名称" width="150"></el-table-column>
      <el-table-column sortable prop="description" label="角色描述" width="300"></el-table-column>
      <el-table-column sortable prop="status" label="用户状态" width="150" align="center">
        <template slot-scope="scope">
          <div><StatusSwitch :mystatus="scope.row.status" :mydisabled="true" @changeSwitch="changeSwitch"></StatusSwitch></div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="250">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
          <el-button size="mini" type="success" @click="menuAccess(scope.$index, scope.row)" :disabled='buttonDisable'>菜单权限</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="40%" @click="closeDialog">
      <el-form label-width="100px" :model="editForm" :rules="rules" ref="editForm" :inline="true">
        <el-form-item label="角色名称" prop="name" width="100">
          <el-input v-model="editForm.name" placeholder="请输入角色名称"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="description" width="100">
          <el-input v-model="editForm.description" placeholder="请输入角色描述"></el-input>
        </el-form-item>
        <el-form-item sortable prop="status" label="启用状态" width="100" size="small" align="center">
          <StatusSwitch :mystatus="editForm.status" :mydisabled="false" @changeSwitch="changeSwitch"></StatusSwitch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog('edit')">取消</el-button>
        <el-button size="small" type="primary" class="title" @click="submitForm('editForm')">保存</el-button>
      </div>
    </el-dialog>
    <!-- 菜单权限 -->
    <el-dialog title="菜单权限" :visible.sync="menuAccessshow" width="30%" @click='closeDialog("perm")'>
     <MenuTree :showCheckbox="true" :roleId="saveroleId" @callFather = "callChecked"></MenuTree>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeDialog("perm")'>取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="menuPermSave">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '../../components/Pagination';
import StatusSwitch from "../../components/orgComponet/StatusSwitch.vue";
import MenuTree from "../../components/MenuTree.vue";
export default {
  data() {
    return {
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      menuAccessshow: false, //控制数据权限显示与隐藏
      title: '添加',
      isdisabled: '',
      buttonDisable: false,
      editForm: {
        id:'',
        name:'',
        description:'',
        status:'',
        token: localStorage.getItem('logintoken')
      },
      // rules 表单验证
      rules: {
        systemNo: [
          { required: true, message: '请输入系统编码', trigger: 'blur' }
        ],
        roleNo: [
          { required: true, message: '请输入角色代码', trigger: 'blur' }
        ],
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ]
      },
      formInline: {
        id:'',
        name:'',
        description:'',
        status:'',
        token: localStorage.getItem('logintoken')
      },
      // 删除
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken')
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 数据权限
      RoleRight: [],
      RoleRightProps: {
        children: 'children',
        label: 'name'
      },
      // 菜单选中结点id
      checkmenu: [],
      //参数role
      saveroleId: '',
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      },
      multipleSelection: []
    }
  },
  // 注册组件
  components: {
    Pagination,
    StatusSwitch,
    MenuTree,
  },
  watch: {},
  created() {
    this.getdata(this.formInline)
  },
  methods: {
    // 获取角色
    getdata() {
      this.loading = false
      this.$axios.get('/api/role/'+this.pageparm.currentPage+'/'+this.pageparm.pageSize).then(res => {
      console.log(res);
      if(res.data.code == 200){
        this.listData = res.data.data.list
        // 分页赋值
        this.pageparm.total = res.data.data.total
      }
      }).catch();
    },
    // 分页插件事件
    callFather(parm) {
      this.pageparm.currentPage = parm.currentPage
      this.pageparm.pageSize = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search() {
      this.$axios.post('/api/role/search',this.formInline).then(res => {
        console.log(res);
        if(res.data.code == 200){
          this.listData = res.data.data
          this.pageparm.total = res.data.data.length
        }
      }).catch(()=>{});
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.isdisabled = true
        this.title = '修改角色'
        this.editForm.no = row.no
        this.editForm.name = row.name
        this.editForm.description = row.description
        this.editForm.status = row.status
      } else {
        this.isdisabled = false
        this.title = '新增角色'
        this.editForm.no = ''
        this.editForm.name = ''
        this.editForm.description = ''
        this.editForm.status = ''
      }
    },
    // 编辑、增加页面保存方法
    submitForm() {
      if(this.title=='新增角色'){
        this.$axios.post('/api/role/add',this.editForm).then(res => {
          console.log(res);
          if(res.data.code == 200){
            this.$message({
              type: 'success',
              message: '添加成功!'
            });
          }
          this.getdata();
          this.editFormVisible = false
        }).catch();
      }else{
        this.$axios.put('/api/role/update',this.editForm).then(res => {
          console.log(res);
          if(res.data.code == 200){
            this.$message({
              type: 'success',
              message: '修改成功!'
            });
          }
          this.getdata();
          this.editFormVisible = false
        }).catch();
      }
    },
    // 删除用户
    deleteUser(index, row) {
      console.log(row.id);
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$axios.delete('/api/role/delete/'+row.id).then(res => {
            console.log(res);
            if(res.data.code == 200){
              this.$message({
                type: 'success',
                message: '删除成功'
              })
              this.getdata();
            }
          }).cathch(()=>{});
        })
      .catch(()=>{})
    },
    // 数据权限
    menuAccess: function(index, row) {
      this.menuAccessshow = true
      this.saveroleId = row.id
    },
    // 关闭编辑、增加弹出框
    closeDialog(dialog) {
      if (dialog == 'edit') {
        this.editFormVisible = false
      } else if (dialog == 'perm') {
        this.menuAccessshow = false
      }
    },
    changeSwitch(e){
        console.log("父组件接收到的值：",e);
        this.editForm.status = e
    },
    //子组件选中的节点信息，传入父组件
    callChecked(val){
      this.checkmenu = val
    },
    // 菜单权限-保存
    menuPermSave() {
      let parm = {
        roleId: this.saveroleId,
        moduleIds: this.checkmenu
      }
      console.log('传后台',parm);
      this.$axios.post('/api/role/schangeMenus',parm).then(
        res=>{
          console.log(res);
          if (res.data.code == 200) {
             this.$message({
              type: 'success',
              message: res.data.message
            })
          }
          this.menuAccessshow = false
        }
      ).catch();
    },
    //获取被勾选的角色的id
    handleSelectionChange(val) {
      for(let index in val){
        this.multipleSelection[index] = val[index].id;
      }
      console.log(this.multipleSelection);
    },
    //批量删除
    batchDelete(){
      console.log(this.multipleSelection);
      if(this.multipleSelection.length==0){
            this.$message({
                type: "info",
                message: "请勾选需要删除的列",
            });
            return;
        }
        this.$confirm("确定要删除吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        })
        .then(() => {
            this.$axios.put("/api/role/batchDelete",this.multipleSelection)
            .then((res) => {
              if (res.data.code == 200) {
                this.$message({
                  type: "success",
                  message: "已删除!",
                });
                this.getdata();
              } else {
                this.$message({
                  type: "info",
                  message: res.data.message,
                });
              }
            })
            .catch((err) => {
              this.$message.error("删除失败，请稍后再试！");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    resetPage(){
      this.pageparm.currentPage = 1
      this.pageparm.pageSize = 10
      this.getdata();
    },
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 